﻿using UnityEngine;
using System.Collections;

public class Coach : MonoBehaviour {

	private float modSpeed = 0.02f;
	private float modSlow = -0.005f;
	private int modDuration = 100;

	public float speed = 0.01f;

	public bool honk = false;
	public bool honk2 = false;

	private float acceleration = 0;

	public GameObject Audio1;
	public GameObject Audio2;
	public GameObject Audio3;
	public GameObject Audio4;
	public GameObject Audio5;
	public GameObject Audio6;

	private float maxSpeed = 0.03f;

	private float minSpeed = 0.009f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		move();
	}

	public float getSpeed() {
		return	speed;
	}
	
	public void setSpeed(float speed) {
		this.speed = speed;

		if (this.speed >  maxSpeed) {
			this.speed = maxSpeed;
			setAcceleration (0);
		} else if (this.speed < minSpeed) {
			this.speed = minSpeed;
			setAcceleration (0);
		}

	}
	
	public float getAcceleration() {
		return acceleration;
	}
	
	public void setAcceleration(float acceleration) {
		this.acceleration = acceleration;
	}
	
	public void move() {

		listenForKeyPresses ();

		setSpeed (getSpeed () + getAcceleration ());
		this.gameObject.transform.Translate (
			speed,
			0,
			0
			);
	}

	void OnTriggerEnter2D(Collider2D other) {
		/*Debug.Log("In the ThingR");
		
		if (other.gameObject.GetComponent<Jogger> () == null) {
			return;
		}

		Jogger jogger = other.gameObject.GetComponent<Jogger> ();
		Debug.Log("In the ThingQ " + modSpeed);
		jogger.setSpeedBoost (modSpeed);
		*/
	}
	
	void OnTriggerExit2D(Collider2D other) {
		//Debug.Log("In the ThingX");
	}

	void OnTriggerStay2D(Collider2D other) {
		if ((!honk&&!honk2) || other.GetComponent<Jogger> () == null) {
			return;
		}

		Jogger jogger = other.gameObject.GetComponent<Jogger> ();

		if (honk) jogger.setSpeedBoost ( modSpeed );		

		if (honk2) jogger.setPantDuration ( modDuration );		

	}


	void listenForKeyPresses () {

		if (Audio2.audio.isPlaying==true)
			Audio1.audio.mute = true;
		else Audio1.audio.mute = false;

		if ((honk== false)&&(Audio4.audio.isPlaying))
		{
			Audio4.audio.Stop();
			Audio5.audio.Play();
		}

		if ((honk2== false)&&(Audio6.audio.isPlaying))
		{
			Audio6.audio.Stop();
			Audio5.audio.Play();
		}

		if (Input.GetKey (KeyCode.RightArrow)) {
			//Audio1.audio.mute = true;
			if (Audio2.audio.isPlaying==false)
			Audio2.audio.Play();

			acceleration = 0.01f;
		} else {
			acceleration = -0.0001f;
		}

		if (Input.GetKey (KeyCode.Space)) {
			honk = true;
			Audio3.audio.Play();

			Audio4.audio.PlayScheduled(0.05d);
			Audio4.audio.loop = true;


		} else {
			honk = false;
		}

		if (Input.GetKey (KeyCode.LeftAlt)) {
			honk2 = true;
			Audio3.audio.Play();
			
			Audio6.audio.PlayScheduled(0.05d);
			Audio6.audio.loop = true;
				
		} else {
			honk2 = false;
		}


		if (Input.GetKey(KeyCode.Escape)) {
			Application.Quit();
		}


	}

}
